package com.matt.controller.web.photo;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.photoalbum.PhotoAlbum;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.Photo.PhotoAlbumService;
import com.matt.service.Photo.PhotoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/web/photo_album")
@Slf4j
public class WebPhotoAlbumController {

    @Autowired
    private PhotoAlbumService photoAlbumService;

    @Autowired
    private PhotoService photoService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "获取相册列表")
    @GetMapping("/albums")
    public StatusResponse getAlbumList(@RequestHeader(TICKET) String ticket, String classId ,Integer type){
        StaffDTO staff = cache.getStaff(ticket);

        return StatusResponse.success( photoAlbumService.list(staff.getAreaId(), classId, type));
    }

    @ApiOperation(value = "增加或变更一个相册")
    @PostMapping("/album")
    public StatusResponse addAlbum(@RequestHeader(TICKET) String ticket,
                                   @RequestBody PhotoAlbum album){
        StaffDTO staff = cache.getStaff(ticket);

        PhotoAlbum ret = null;
        album.setAreaId(staff.getAreaId());
        if (StringUtils.isNotEmpty(album.getId())){
            ret = photoAlbumService.update(album);
        }else {
            ret = photoAlbumService.add(album);
        }

        return StatusResponse.success(ret);
    }



    @ApiOperation(value = "获取一个相册对象")
    @GetMapping("/album")
    public StatusResponse getAlbum(@RequestHeader(TICKET) String ticket, String albumId ,Integer type){
        StaffDTO staff = cache.getStaff(ticket);
        return StatusResponse.success(photoAlbumService.get(staff.getAreaId(), albumId,type));
    }

    @ApiOperation(value = "逻辑删除一个或多个相册对象")
    @DeleteMapping("/album")
    public StatusResponse deleteAlbum(@RequestHeader(TICKET) String ticket, String[] albumIds ){
        StaffDTO staff = cache.getStaff(ticket);

        photoAlbumService.deleteAll(staff.getAreaId(), albumIds);

        return StatusResponse.success();
    }

    @ApiOperation(value = "获取相册下的照片，添加时间倒序")
    @GetMapping("/photos")
    public StatusResponse getPhotos(@RequestHeader(TICKET) String ticket, String albumId, PagerQuery pagerQuery ){
        StaffDTO staff = cache.getStaff(ticket);

        return StatusResponse.success(photoService.list(staff.getAreaId(), albumId, pagerQuery));
    }


    @ApiOperation(value = "增加一个照片")
    @PostMapping("/upload")
    public StatusResponse getAlbumTemplates(@RequestHeader(TICKET) String ticket,
                                            String title, String description, String albumId , MultipartFile[] files){
        if (files == null || files.length < 1){
            return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "参数不能为空");
        }

        StaffDTO staff = cache.getStaff(ticket);

        return StatusResponse.success(photoService.addMultiple(staff.getAreaId(), albumId, staff.getId(), title, description, files ));
    }

    @ApiOperation(value = "修改一个照片, albumId只能单独传")
    @PostMapping("/photo")
    public StatusResponse updatePhoto(@RequestHeader(TICKET) String ticket, String photoId, String title, String description,  String newAlbumId  ){
        StaffDTO staff = cache.getStaff(ticket);

        if (StringUtils.isNotEmpty(newAlbumId)){
             photoService.move(staff.getAreaId(), photoId, newAlbumId);
            return StatusResponse.success();
        }

        photoService.update(staff.getAreaId(), photoId, staff.getId(), photoId);

        return StatusResponse.success();
    }

    @ApiOperation(value = "删除相册下一个或多个照片")
    @DeleteMapping("/photo")
    public StatusResponse deletePhoto(@RequestHeader(TICKET) String ticket, String albumId, String[] photoIds ){
        StaffDTO staff = cache.getStaff(ticket);

        photoService.deleteAll(staff.getAreaId(), albumId, staff.getId(), photoIds);

        return StatusResponse.success();
    }

}
